package com.example.flashsales.controller;

import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.example.flashsales.Tools.Sample;
import com.example.flashsales.Tools.NumberUtil;
import com.example.flashsales.Tools.SMSTools;
import com.example.flashsales.entity.Result;
import com.example.flashsales.entity.dto.SMS;
import com.example.flashsales.entity.dto.UserDto;
import com.example.flashsales.entity.pojo.Store;
import com.example.flashsales.service.impl.StoreServiceImpl;
import com.example.flashsales.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/sms")
public class SampleController {

    @Autowired
    UserServiceImpl userService;

    @Autowired
    StoreServiceImpl storeService;

    private Map<String,Integer> map = new HashMap<>();

    @ResponseBody
    @PostMapping("/sendSMS")
    public Result sendSMS(@RequestBody SMS sms) throws Exception {
        if (!NumberUtil.isPhone(sms.getPhone())){
            return Result.error("请输入正确的手机号");
        }
        com.aliyun.dysmsapi20170525.Client client = Sample.createClient("LTAI5t6xPwCfzfHX5NKjcSut", "6FDcCIvxFBfCso8zbX2kYG9GusfPyJ");
        final int sms1 = SMSTools.createSMS();
        map.put(sms.getPhone(),sms1);
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setSignName("阿里云短信测试")
                .setTemplateCode("SMS_154950909")
                .setPhoneNumbers(sms.getPhone())
//                .setTemplateParam("{\"code\":\"1224\"}");
                .setTemplateParam("{code:"+sms1+"}");
        System.out.println(client.sendSms(sendSmsRequest));
        // 复制代码运行请自行打印 API 的返回值
        return Result.ok("发送成功");
    }


    /**
     *
     * @param userDto
     * @return
     */
    @PostMapping("/register")
    @ResponseBody
    public Result register(@RequestBody UserDto userDto){
//        System.out.println(map);
        if (userDto == null){
            return Result.error("数据不能为空");
        }
        if(userDto.getCode() == null || userDto.getCode().equals("")){
            userDto.setStoreId(0);
        }else {
            final Store store = storeService.viewStoreMessageByInviteCode(userDto.getCode());
            if (store == null){
                return Result.error("注册失败,店家不存在");
            }
            userDto.setStoreId(store.getId());
        }
        if (!map.containsKey(userDto.getPhone())){
            return Result.error("手机号码错误");
        }
        if (map.get(userDto.getPhone()) != userDto.getSms()){
            return Result.error("验证码错误");
        }
        final String register = userService.register(userDto);
        if (register.equals("注册成功")){
            map.remove(userDto.getPhone());
            return Result.ok(register);
        }
        return Result.error(register);
    }

}
